package mini_app

import (
	"context"
	"mini-app/internal/app/grpc/protoc/mini_app"
	"mini-app/internal/dal"
	"mini-app/internal/dao"
)

// GetAdvertisementOrder 查询广告订单内容
func (*MiniAppService) GetAdvertisementOrder(ctx context.Context, req *mini_app.GetAdvertisementOrderReq) (*mini_app.GetAdvertisementOrderRsp, error) {
	res := &mini_app.GetAdvertisementOrderRsp{}

	// 参数验证
	if req.Id == "" {
		res.Code = -1
		res.Msg = "广告订单ID不能为空"
		return res, nil
	}

	// 查询广告订单详情
	order, err := dao.Advertisement{}.Detail(ctx, dal.Q, req.Id)
	if err != nil {
		res.Code = -1
		res.Msg = "查询广告订单失败: " + err.Error()
		return res, nil
	}

	if order == nil {
		res.Code = -1
		res.Msg = "广告订单不存在"
		return res, nil
	}

	// 构建响应数据
	res.Code = 0
	res.Msg = "success"
	res.Data = &mini_app.AdvertisementOrderInfo{
		Id:              order.ID,
		CreatedAt:       order.CreatedAt,
		CustomName:      order.CustomName,
		PhoneNumber:     order.PhoneNumber,
		DeviceIds:       order.DeviceIDs,
		StartAt:         order.StartAt,
		EndAt:           order.EndAt,
		Status:          int32(order.Status),
		Remark:          order.Remark,
		Openid:          order.OpenID,
		AdvertisementId: order.AdvertisementID,
		PayAmount:       order.PayAmount,
		PaidAmount:      order.PaidAmount,
		PaidId:          order.PaidID,
		DiscountAmount:  order.DiscountAmount,
		ScheduleId:      order.ScheduleID,
	}

	return res, nil
}

// GetAdvertisementOrder 查询广告订单内容
func (*MiniAppService) SetScheduleInfo(ctx context.Context, req *mini_app.SetScheduleInfoReq) (*mini_app.SetScheduleInfoRsp, error) {
	res := &mini_app.SetScheduleInfoRsp{}

	err := dao.Advertisement{}.OrderScheduleStatusChange(ctx, dal.Q, req.OrderId, int(req.Status))
	if err != nil {
		res.Code = -1
		res.Msg = "设置订单状态失败: " + err.Error()
		return res, nil
	}
	return res, nil

}
